﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Microsoft.SharePoint.Administration;

namespace SharePointCommander.Model.TreeView
{
    public class SCFarm : SCTreeViewItem<SPFarm>
    {
        public SCFarm(SCTreeViewItem parent, SPFarm farm) : base(parent, farm)
        {
            Header = farm.DisplayName;

            Expanded += new System.Windows.RoutedEventHandler(SCFarm_Expanded);
            ItemsSource = new object[] { null };
            IsExpanded = true;
        }

        void SCFarm_Expanded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!ItemsSource.OfType<SCProperties>().Any())
            {
                SPWebServiceCollection webServices = new SPWebServiceCollection(SPObject);

                ItemsSource =
                    (from ws in webServices select new SCWebService(this, ws) as TreeViewItem)
                    .Union(
                        new[] { 
                        new SCServerCollection(this, SPObject.Servers) as TreeViewItem,
                        new SCServiceCollection(this, SPObject.Services) as TreeViewItem,
                        new SCSolutionCollection(this, SPObject.Solutions) as TreeViewItem,
                        new SCProperties(this, SPObject.Properties) as TreeViewItem
                    }
                );
            }
        }

    }
}
